package cn.supermaster.management.web.service.system.impl;

import cn.supermaster.management.common.constant.UserConstant;
import cn.supermaster.management.common.domain.SystemDepartment;
import cn.supermaster.management.common.domain.SystemRole;
import cn.supermaster.management.common.entity.Ztree;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.common.utils.CheckUtils;
import cn.supermaster.management.common.utils.ShiroUtils;
import cn.supermaster.management.common.utils.TextUtils;
import cn.supermaster.management.common.utils.WebHelper;
import cn.supermaster.management.system.service.ISystemDepartmentService;
import cn.supermaster.management.web.service.system.ISDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.List;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.service.system.impl
 * @ClassName: SDepartmentServiceImpl
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/2/19 22:03
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class SDepartmentServiceImpl implements ISDepartmentService {

    private String prefix = "system/dept";

    @Autowired
    private ISystemDepartmentService systemDepartmentService;

    /*
     * ================================================
     *  页面跳转功能实现
     * ================================================
     */

    /**
     * @Description: []
     * <p></P>
     * @Title: skipDepartmentIndexHtml
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Return: java.lang.String
     */
    @Override
    public String skipDepartmentIndexHtml() {
        return prefix + "/dept";
    }

    /**
     * @Description: []
     * <p></P>
     * @Title: skipDepartmentAddHtml
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: modelMap
     * @Param: parentId
     * @Return: java.lang.String
     */
    @Override
    public String skipDepartmentAddHtml(ModelMap modelMap, Long parentId) {
        modelMap.put("dept", systemDepartmentService.selectDeptById(parentId));
        return prefix + "/add";
    }

    /**
     * @Description: []
     * <p></P>
     * @Title: skipDepartmentModifyHtml
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: modelMap
     * @Param: deptId
     * @Return: java.lang.String
     */
    @Override
    public String skipDepartmentModifyHtml(ModelMap modelMap, Long deptId) {
        SystemDepartment dept = systemDepartmentService.selectDeptById(deptId);
        if (CheckUtils.isNotNull(dept) && 100L == deptId) {
            dept.setParentName("无");
        }
        modelMap.put("dept", dept);
        return prefix + "/edit";
    }

    /**
     * @Description: []
     * <p></P>
     * @Title: skipBuildDepartmentTree
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: modelMap
     * @Param: deptId
     * @Param: excludeId
     * @Return: java.lang.String
     */
    @Override
    public String skipBuildDepartmentTree(ModelMap modelMap, Long deptId, String excludeId) {
        modelMap.put("dept", systemDepartmentService.selectDeptById(deptId));
        modelMap.put("excludeId", excludeId);
        return prefix + "/tree";
    }

    /*
     * ================================================
     *  Rest功能实现
     * ================================================
     */

    /**
     * @Description: []
     * <p></P>
     * @Title: queryDepartmentList
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: department
     * @Return: java.util.List<cn.supermaster.management.common.domain.SystemDepartment>
     */
    @Override
    public List<SystemDepartment> queryDepartmentList(SystemDepartment department) {
        List<SystemDepartment> deptList = systemDepartmentService.selectDeptList(department);
        return deptList;
    }

    /**
     * @Description: []
     * <p></P>
     * @Title: addDepartment
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: department
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse addDepartment(SystemDepartment department) {
        if (UserConstant.DEPT_NAME_NOT_UNIQUE.equals(systemDepartmentService.checkDeptNameUnique(department))) {
            return WebResponse.failure("新增部门'" + department.getDeptName() + "'失败，部门名称已存在");
        }
        department.setCreateBy(ShiroUtils.getuserName());
        return WebHelper.webResult(systemDepartmentService.insertDept(department));
    }

    /**
     * @Description: []
     * <p></P>
     * @Title: modifyDepartment
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: department
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse modifyDepartment(SystemDepartment department) {
        if (UserConstant.DEPT_NAME_NOT_UNIQUE.equals(systemDepartmentService.checkDeptNameUnique(department))) {
            return WebResponse.failure("修改部门'" + department.getDeptName() + "'失败，部门名称已存在");
        } else if (department.getParentId().equals(department.getDeptId())) {
            return WebResponse.failure("修改部门'" + department.getDeptName() + "'失败，上级部门不能是自己");
        } else if (TextUtils.equals(UserConstant.DEPT_DISABLE, department.getStatus())
                && systemDepartmentService.selectNormalChildrenDeptById(department.getDeptId()) > 0) {
            return WebResponse.failure("该部门包含未停用的子部门！");
        }
        department.setUpdateBy(ShiroUtils.getuserName());
        return WebHelper.webResult(systemDepartmentService.updateDept(department));
    }

    /**
     * @Description: []
     * <p></P>
     * @Title: deleteDepartment
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: deptId
     * @Return: cn.supermaster.management.common.response.WebResult
     */
    @Override
    public WebResponse deleteDepartment(Long deptId) {
        if (systemDepartmentService.selectDeptCount(deptId) > 0) {
            return WebResponse.warn("存在下级部门,不允许删除");
        }
        if (systemDepartmentService.checkDeptExistUser(deptId)) {
            return WebResponse.warn("部门存在用户,不允许删除");
        }
        return WebHelper.webResult(systemDepartmentService.deleteDeptById(deptId));
    }

    /**
     * @Description: []
     * <p></P>
     * @Title: checkDepartmentNameUnique
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: dept
     * @Return: java.lang.String
     */
    @Override
    public String checkDepartmentNameUnique(SystemDepartment dept) {
        return systemDepartmentService.checkDeptNameUnique(dept);
    }

    /**
     * @Description: [加载部门列表树]
     * <p></P>
     * @Title: loadAllDepartmentTree
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param:
     * @Return: java.util.List<cn.supermaster.management.common.entity.Ztree>
     */
    @Override
    public List<Ztree> loadAllDepartmentTree() {
        List<Ztree> ztrees = systemDepartmentService.selectDeptTree(new SystemDepartment());
        return ztrees;
    }

    /**
     * @Description: [加载部门列表树（排除下级）]
     * <p></P>
     * @Title: loadExcludeChildDepartmentTree
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: excludeId
     * @Return: java.util.List<cn.supermaster.management.common.entity.Ztree>
     */
    @Override
    public List<Ztree> loadExcludeChildDepartmentTree(Long excludeId) {
        SystemDepartment dept = new SystemDepartment();
        dept.setDeptId(excludeId);
        List<Ztree> ztrees = systemDepartmentService.selectDeptTreeExcludeChild(dept);
        return ztrees;
    }

    /**
     * @Description: [加载角色部门（数据权限）列表树]
     * <p></P>
     * @Title: loadRoleDepartmentTree
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: role
     * @Return: java.util.List<cn.supermaster.management.common.entity.Ztree>
     */
    @Override
    public List<Ztree> loadRoleDepartmentTree(SystemRole role) {
        List<Ztree> ztrees = systemDepartmentService.roleDeptTreeData(role);
        return ztrees;
    }
}